# -*- coding: utf-8 -*-
import serial,sys,time,json,redis,binascii,traceback
# flog=open("/home/wwwroot/pavgps/temperature.log",'w')
portType=0
conf={"drctPort":"COM9"}
try:
    rd = redis.Redis(host='127.0.0.1',port=6379)
    # conffile = open('/home/wwwroot/pavgps/config.json','r')
    # confstr = conffile.read()
    # conf = json.loads(confstr)
    ser = serial.Serial(conf["drctPort"], 9600, timeout=0.5)

except Exception as e:
    print("============")
    errstr=traceback.print_exc()
    # flog.write(""+repr(e));
    # flog.close()
    quit()

def getDrct(ser):
    temp = -1
    try:
        if ser!=None:
            cmd = bytes.fromhex("7704000307")
            ser.write(cmd)
            n=ser.inWaiting()                             #串口接收
            if n:
                data= str(binascii.b2a_hex(ser.read(n)))[2:-1]          #Hex转换成字符串
                hasstr =data.find('77070083')
                print(data)
                if hasstr>-1:
                    fdata =data[8:14]
                    return int(fdata)/100

    except:
        temp = 0
    print(temp)
    return temp

while(1):
    tmp = getDrct(ser)
    if tmp>-0:
         print("DRCT: ", tmp)
         rd.set("drct",tmp,10)
    time.sleep(3)
